package loader;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Properties;

public class ReflectTest {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException, ClassNotFoundException, InstantiationException {
        // 使用反射可以完成一些 非常规操作往下面集合中添加string
        ArrayList<Integer> array = new ArrayList<>();
        array.add(12);
        array.add(12);
        array.add(22);

        Class<? extends ArrayList> c = array.getClass();
        Method add = c.getDeclaredMethod("add", Object.class);
        add.invoke(array, "23");
        add.invoke(array, "asdf");
        System.out.println(array);
        //通过配置问价运行文件
        /*
            class.txt
            className=
            methodName=
         */
        //加载数据
        Properties prop = new Properties();
        FileReader fr = new FileReader("funCode\\class.txt");
        // 加载到了集合中
        prop.load(fr);

        String className = prop.getProperty("className");
        String method = prop.getProperty("methodName");
        //通过反射来使用

        Class<?> aClass = Class.forName(className);

        Object o = aClass.getConstructor().newInstance();

        Method declaredMethod = aClass.getDeclaredMethod(method);
        declaredMethod.setAccessible(true);
        declaredMethod.invoke(o);


        fr.close();
    }
}
